package tk.leafotto.salarybill.swing;

/**
 * Created by leafo on 2017/5/17.
 */
import javax.swing.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.awt.*;

/**
 *
 * 类描述：
 * 测试日志输出到UI组件
 * @version 1.0
 */
public class LogDemoJFrame extends JFrame {

    private JPanel jPanel;

    private JLabel logLabel;
    private JScrollPane logScrollPane;
    private JTextArea logTextArea;
    private final static Log log = LogFactory.getLog(LogDemoJFrame.class);

    public LogDemoJFrame() {

        logScrollPane = new JScrollPane();
        logTextArea = new JTextArea();
        logLabel = new JLabel("数据文件：",JLabel.LEFT);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        jPanel = new JPanel();
        jPanel.add(logLabel);

//        jPanel.add(new JTextField(40));
//        jPanel.add(new JButton("浏览"));

        logTextArea.setColumns(50);
        logTextArea.setRows(15);
        logScrollPane.setViewportView(logTextArea);

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jPanel, GroupLayout.DEFAULT_SIZE, 610, Short.MAX_VALUE)
                                .addContainerGap())
                        .addComponent(logScrollPane, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 630, Short.MAX_VALUE));
        layout.setVerticalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap().addComponent(jPanel, GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(logScrollPane, GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)));
        pack();
    }

    public void initLog() {
        try {
            Thread t1, t2;
//            t1 = new LabelLogAppender(logLabel);
            t2 = new TextAreaLogAppender(logTextArea, logScrollPane);
//            t1.start();
            t2.start();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e, "绑定日志输出组件错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    public static void main(String[] s) {

        JPanel jPanel = new JPanel();
        jPanel.add(new JButton("Button"));
        jPanel.setVisible(true);

        LogDemoJFrame logDemoFrame = new LogDemoJFrame();
        Container contentPane = logDemoFrame.getContentPane();
        logDemoFrame.initLog();
        logDemoFrame.setVisible(true);
        for (int i = 0; i < 100; i++) {
            log.info("测试日志输出:" + i);
        }
    }
}